package com.yupi.springbootinit.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.manager.StrategySelector;
import com.yupi.springbootinit.model.dto.reverse.ServerLoadInfo;
import com.yupi.springbootinit.model.entity.Chart;
import com.yupi.springbootinit.model.vo.BiResponse;
import com.yupi.springbootinit.service.ChartService;
import com.yupi.springbootinit.mapper.ChartMapper;
import com.yupi.springbootinit.service.GenChartStrategy;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
    implements ChartService{

    @Resource
    private ChartMapper chartMapper;
    @Resource
    @Lazy
    private StrategySelector strategySelector;

    // 删除分表
    @Override
    public void deleteTable(Long id) {
        chartMapper.deleteTable(id);
    }

    // 将用户上传文件数据分表创建并存入一个新表(建表)
    @Override
    public void createChartHeader(Long id, String[] headerData) {
        chartMapper.createChartHeader(id,headerData);
    }

    // 插入用户上传文件数据分表数据
    @Override
    public void insertChartData(Map<String, Object> map) {
        chartMapper.insertChartData(map);
    }

    // 查询用户上传的文件原始数据
    @Override
    public List<Map<String, Object>> selectCsvData(Long id) {
        return chartMapper.queryChartData(id);
    }

    // 将用户上传文件数据分表创建并存入一个新表
    public void setChartData(String csvData, Long id){
        try {
            String[] header = csvData.split("\n");
            String[] headerData = header[0].split(",");
            // 将用户上传文件数据分表创建并存入一个新表(建表)
            createChartHeader(id, headerData);

            Map<String, Object> map = new HashMap<>();
            List<String> list1 = new ArrayList<>();
            map.put("id", id);

            for (int i = 0; i < headerData.length; i++) {
                list1.add(headerData[i]);
            }
            map.put("tableParams", list1);
            for (int k = 1; k < header.length; k++) {
                List<String> list2 = new ArrayList<>();
                for (int j = 0; j < headerData.length; j++) {
                    String[] tableData = header[k].split(",");
                    list2.add(tableData[j]);
                }
                map.put("tableParamsValue", list2);
                // 插入用户上传文件数据分表数据
                insertChartData(map);
            }
        } catch (Exception exception) {
            log.error("插入文件数据错误");
            exception.printStackTrace();
        }
    }

    // 查询数据库是否存在此表
    @Override
    public boolean queryTableNumber(Long chartId){
        int tableNumber = chartMapper.queryTableNumber("chart_" + chartId);
        if (tableNumber == 0) return true;
        return false;
    }

    // 获取原始表格的数据
    @Override
    public String getTableData(Long chartId) {
        List<Map<String, Object>> maps = chartMapper.selectCsvData(chartId);
        StringBuilder stringBuilder = new StringBuilder();
        // 先拼接表头
        for (Map<String, Object> resultMap : maps) {
            for (Map.Entry<String, Object> entry : resultMap.entrySet()) {
                // 以逗号分隔的形式进行拼接,在字符串最后添加一个换行符
                stringBuilder.append(StringUtils.join(entry.getKey(), ","));
            }
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
            stringBuilder.append("\n");
            break;
        }
        // 再拼接内容
        int a = 0;
        for (Map<String, Object> resultMap : maps) {
            if (a == 0){
                a++;
                continue;
            }
            for (Map.Entry<String, Object> entry : resultMap.entrySet()) {
                // 以逗号分隔的形式进行拼接,在字符串最后添加一个换行符
                stringBuilder.append(StringUtils.join(entry.getValue(), ","));
            }
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
            stringBuilder.append("\n");
        }
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);

        System.out.println("-----到这里了1------------------");
        System.out.println(stringBuilder.toString());
        System.out.println("-----到这里了2------------------");
        return stringBuilder.toString();
    }

    // @TODO 反向压力
    @Override
    public BiResponse genChart(Chart chartEntity, ServerLoadInfo info) {
        GenChartStrategy genChartStrategy = strategySelector.selectStrategy(info);
        return genChartStrategy.executeGenChart(chartEntity);
    }
}




